package io.a.producer;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class PulsarProducer {


    private static final String localClusterUrl = "pulsar://pulsar-d1-dev.guideirai.online:26650";


    public static void main(String[] args) {
        try {
            Producer<byte[]> producer = getProducer();
            Map<String, Object> map = new HashMap<>();
            map.put("type", "trafficEvent");
            map.put("payload", "hi");

//            String msg = "{'type':'gsdt','payload':'hi'}";

            long start = System.currentTimeMillis();
            producer.send(JSON.toJSONString(map).getBytes());
            long end = System.currentTimeMillis();
            log.info("send time: {} ms", end - start);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static Producer<byte[]> getProducer() throws Exception {
        PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(localClusterUrl).build();
        return pulsarClient.newProducer().topic("data_sync_ai_platform_event").create();
    }


}
